package com.wwj.core.wawa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wwj.core.wawa.entity.PlayRecord;
import com.wwj.core.wawa.entity.Player;
import com.wwj.core.wawa.entity.WaWaMachine;
import com.wwj.core.wawa.entity.mapper.PlayRecordMapper;
import com.wwj.core.wawa.service.IPlayRecordService;
import com.wwj.core.wawa.service.IPlayerService;
import com.wwj.core.wawa.service.IWaWaMachineService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/3.
 */
@Service
public class PlayRecordServiceImpl implements IPlayRecordService {

    @Resource
    private PlayRecordMapper playRecordMapper;

    @Resource
    private IWaWaMachineService waWaMachineService;

    @Resource
    private IPlayerService playerService;

    @Override
    public Page<PlayRecord> findByPage(Map<String, Object> params, int offset, int limit) {
        PageHelper.offsetPage(offset, limit);
        return (Page<PlayRecord>) playRecordMapper.selectPlayRecordsByParams(params);
    }

    @Override
    public PlayRecord findById(Integer id) {
        return playRecordMapper.selectPlayRecordById(id);
    }

    @Override
    public Page<PlayRecord> findByPageUserInfo(Map<String, Object> params, int offset, int limit) throws Exception {
        PageHelper.offsetPage(offset, limit);
        return (Page<PlayRecord>) playRecordMapper.selectPlayRecordsUserInfoByParams(params);
    }

    @Override
    public PlayRecord findByToken(String token) throws Exception {
        return playRecordMapper.selectPlayRecordByToken(token);
    }

    @Override
    public int updatePlayRecord(PlayRecord playRecord) throws Exception {
        playRecordMapper.updatePlayRecord(playRecord);
        return 1;
    }

    @Override
    public int updatePlayRecordForResult(Player player, PlayRecord playRecord) throws Exception {
        playRecordMapper.updatePlayRecord(playRecord);
        if(null != player) {
            if(playRecord.getPlayResult() == PlayRecord.RESULT_SUCCESS)
                playerService.updatePlayerPlaySuccess(player.getId());
            else if (playRecord.getPlayResult() == PlayRecord.RESULT_FAILED)
                playerService.updatePlayerPlayFailed(player.getId());
        }
        return 1;
    }

    @Override
    public int updatePlayRecordForStartGame(PlayRecord playRecord, String deviceId) throws Exception {
        WaWaMachine machine = waWaMachineService.findByDeviceId(deviceId);
        if (machine == null)
            throw new Exception("娃娃机「device_id=" + deviceId + "」数据不存在");
        if (machine.getStatus() != WaWaMachine.STATUS_IDLE)
            throw new Exception("娃娃机「device_id=" + deviceId + "」非空闲状态");
        machine.setStatus(WaWaMachine.STATUS_PROGRESS);
        machine.setCurrentPlayerId(playRecord.getPlayerId());
        waWaMachineService.updateMachine(machine);
        playRecordMapper.updatePlayRecord(playRecord);
        return 1;
    }
}
